<?php

/**
 * JFusion User Class for copix
 *
 * PHP version 5
 *
 * @version    $Id: ###FILE_NAME### ###REVISION### $
 * @category   JFusion
 * @package    JFusionPlugins
 * @subpackage copix
 * @author     Emmanuel DENIS
 * @copyright  ###COPYRIGHT###
 * @license    ###LICENSE###
 * @link       http://www.jfusion.org
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

/**
 * JFusion User Class for Copix
 * For detailed descriptions on these functions please check the model.abstractuser.php
 *
 * @category   JFusion
 * @package    JFusionPlugins
 * @subpackage copix
 * @author     Emmanuel DENIS
 * @copyright  ###COPYRIGHT###
 * @license    ###LICENSE###
 * @link       http://www.jfusion.org
 */
class JFusionUser_copix extends JFusionUser {
    /**
     * returns the name of this JFusion plugin
     * @return string name of current JFusion plugin
     */
    function getJname() {
        return 'copix';
    }
    function &getUser($userinfo) {
        //get the identifier
        $identifier = $userinfo;
        if (is_object($userinfo)) {
            $identifier = $userinfo->username;
        }
        $jname = $this->getJname();
        $my_admin = & JFusionFactory::getAdmin($jname);
        // Get user info from database
        $db = JFusionFactory::getDatabase($jname);
        $query  = 'SELECT id_dbuser, login_dbuser, email_dbuser, password_dbuser, enabled_dbuser';
        $query .= ' FROM '.$my_admin->getTablename();
        $query .= ' WHERE login_dbuser = '.$db->Quote($identifier);
        if ($my_admin->exclude_admins) {
            $query .= ' AND id_dbuser NOT IN (SELECT user_dbgroup FROM dbgroup_users)';
        }
        $db->setQuery($query);
        $copix_user = $db->loadObject();
        if ($copix_user) {
            $user = new stdClass;
            $user->userid   = $copix_user->id_dbuser;
            $user->name     = $copix_user->login_dbuser;
            $user->username = $copix_user->login_dbuser;
            $user->email    = $copix_user->email_dbuser;
            $user->password = $copix_user->password_dbuser;
            $user->group_id = 0;
            if ($copix_user->enabled_dbuser == 1) {
                $user->block = 0; // allowed to access site
            } else {
                $user->block = 1; // user access is blocked
            }
            //TODO: ajouter les autres infos
            //$user->registerdate = date('d-m-Y H:i:s', );
            //$user->lastvisitdate =
            return $user;
        }
        else {
            $result = false;
            return $result;
        }
    }
    function deleteUser($userinfo) {
        $status = array();
        $status['error'] = '';
        $status['debug'] = JText::_('METHOD_NOT_IMPLEMENTED');
        return $status;
    }
    function destroySession($userinfo, $options) {
        require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.jplugin.php';
        $status = JFusionJplugin::destroySession($userinfo, $options, $this->getJname());
        return $status;
    }
    function createSession($userinfo, $options, $framework = true) {
        //do not create sessions for blocked users
        if (!empty($userinfo->block) || !empty($userinfo->activation)) {
            $status = array();
            $status['error'] = array();
            $status['debug'] = array();
            $status['error'][] = JText::_('FUSION_BLOCKED_USER');
            return $status;
        }
        $status = JFusionJplugin::createSession($userinfo, $options, $this->getJname(),'brute_force');
        return $status;
    }
    function filterUsername($username) {
        return $username;
    }
    function updatePassword($userinfo, &$existinguser, &$status) {
        $status = array();
        $status['error'] = '';
        $status['debug'] = JText::_('METHOD_NOT_IMPLEMENTED');
        return $status;
    }
    function createUser($userinfo, &$status) {
        $status['debug'][] = JText::_('UNABLE_CREATE_USER');
    }
    function updateEmail($userinfo, &$existinguser, &$status) {
        //we need to update the email
        $db = JFusionFactory::getDatabase($this->getJname());
        $my_admin = & JFusionFactory::getAdmin($this->getJname());
        $query  = 'UPDATE '.$my_admin->getTablename();
        $query .= ' SET email_dbuser ='.$db->Quote($userinfo->email);
        $query .= ' WHERE id_dbuser ='.(int)$existinguser->userid;
        if ($my_admin->exclude_admins) {
            $query .= ' AND id_dbuser NOT IN (SELECT user_dbgroup FROM dbgroup_users)';
        }
        $db->setQuery($query);
        if (!$db->query()) {
            $status['error'][] = JText::_('EMAIL_UPDATE_ERROR') . $db->stderr();
        } else {
            $status['debug'][] = JText::_('EMAIL_UPDATE') . ': ' . $existinguser->email . ' -> ' . $userinfo->email;
        }
    }
    function activateUser($userinfo, &$existinguser, &$status) {
        //TODO: create a function that un-bans a copix user
        $status['debug'][] = JText::_('METHOD_NOT_IMPLEMENTED');
        return $status;
    }
    function inactivateUser($userinfo, &$existinguser, &$status) {
        //TODO: create a function that bans a copix user
        $status['debug'][] = JText::_('METHOD_NOT_IMPLEMENTED');
        return $status;
    }
}
